class Ball {
    constructor(canvas, img, width, height, index) {
        //scale 是为了解决移动端图片模糊问题 有点乱，后期再整理
        this.scale = window.devicePixelRatio
        //小球随机位置设置到容器下半部分，不至于悬浮在上面
        this.x = this.rand(canvas.width - width * this.scale)
        this.y = this.rand(canvas.height - height * this.scale - (canvas.height / 2)) + canvas.height / 2
        this.img = img
        this.index = index;
        if (width) {
            canvas.getContext('2d').drawImage(this.img, this.x, this.y, width * this.scale, height * this.scale)
        }
        this.init()
    }
    init() {
        do {
            this.speedX = this.rand(20) - 10
        } while (this.speedX < 5)
        do {
            this.speedY = this.rand(20) - 10
        } while (this.speedY < 5)
    }
    rand(num) {
        return Math.random() * num
    }
    run(ctx, canvas, width, height) {
        this.x += this.speedX
        this.y += this.speedY
        if (this.x > canvas.width - width * this.scale) {
            this.speedX = -this.speedX
        }
        if (this.x < 0) {
            this.speedX = Math.abs(this.speedX)
        }
        if (this.y > canvas.height - height * this.scale) {
            this.speedY = -this.speedY
        }
        if (this.y < 0) {
            this.speedY = Math.abs(this.speedY)
        }
        canvas.getContext('2d').drawImage(this.img, this.x, this.y, width * this.scale, height * this.scale)
    }
}
// export default Ball